package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.LoginMethod;
import com.battle.pojo.param.loginMethod.*;
import com.battle.pojo.vo.loginMethod.*;
import com.battle.pojo.dto.loginMethod.*;
import com.battle.service.repository.in.LoginMethodRepository;
import com.battle.service.service.in.LoginMethodService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：登录方式服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class LoginMethodServiceImpl implements LoginMethodService {

    private final LoginMethodRepository loginMethodRepository;

    public LoginMethodServiceImpl(LoginMethodRepository loginMethodRepository) {
        this.loginMethodRepository = loginMethodRepository;
    }


    /**
     * 创建登录方式
     *
     * @param param
     */
    @Override
    public void createLoginMethod(CreateLoginMethodParam param) {
        LoginMethod loginMethod = new LoginMethod();
        BeanUtils.copyProperties(param, loginMethod);
        Long id = IdGenerator.SNOW.generate();
        loginMethod.setId(id);
        loginMethodRepository.create(loginMethod);
    }

    /**
     * 修改登录方式
     *
     * @param param
     */
    @Override
    public void editLoginMethod(EditLoginMethodParam param) {
        Long id = param.getId();
        LoginMethod loginMethod = loginMethodRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, loginMethod);
        loginMethodRepository.edit(loginMethod);
    }

    /**
     * 移除登录方式
     *
     * @param id
     */
    @Override
    public void removeLoginMethod(Long id) {
        loginMethodRepository.removeById(id);
    }

    /**
     * 启动/禁用登录方式
     *
     * @param id
     */
    @Override
    public void enabledLoginMethod(Long id) {
        LoginMethod loginMethod = loginMethodRepository.getByIdCheck(id);
        loginMethod.setIsDisable(!loginMethod.getIsDisable());
        loginMethodRepository.edit(loginMethod);
    }

    /**
     * 获取登录方式详情
     *
     * @param id
     * @return
     */
    @Override
    public GetLoginMethodVo getLoginMethod(Long id) {
        LoginMethod loginMethod = loginMethodRepository.getByIdCheck(id);
        GetLoginMethodVo vo = new GetLoginMethodVo();
        BeanUtils.copyProperties(loginMethod, vo);
        return vo;
    }

    /**
     * 获取登录方式 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetLoginMethodListVo> getLoginMethodList(GetLoginMethodListParam param) {

        SelectLoginMethodDto dto = new SelectLoginMethodDto();
        BeanUtils.copyProperties(param, dto);
        Page<LoginMethod> page = loginMethodRepository.selectLoginMethod(dto);
        List<LoginMethod> loginMethodList = page.getRecords();
        List<GetLoginMethodListVo> list = new ArrayList<>();
        for (LoginMethod loginMethod : loginMethodList) {
            GetLoginMethodListVo vo = new GetLoginMethodListVo();
            BeanUtils.copyProperties(loginMethod, vo);
            vo.setEnable(!loginMethod.getIsDisable());
            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取登录方式下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetLoginMethodDownListVo> getLoginMethodDownList(GetLoginMethodDownListParam param) {
        LoginMethod po = new LoginMethod();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<LoginMethod> loginMethodList = loginMethodRepository.getListByEntity(po);

        List<GetLoginMethodDownListVo> list = new ArrayList<>();
        for (LoginMethod loginMethod : loginMethodList) {
            GetLoginMethodDownListVo vo = new GetLoginMethodDownListVo();
            BeanUtils.copyProperties(loginMethod, vo);
            list.add(vo);
        }
        return list;
    }


}



